/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logextractionconsole;
import java.io.*;
import java.util.*;
import com.eep.logging.*;
/**
 *
 * @author swrosemergy_loc_adm
 */
public class LogExtraction {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

            Calendar cal = Calendar.getInstance();
            boolean argIdentified = false;

            // Create the log files
            for (String s: args)
            {

                // Security is "-s"
                if(s.compareToIgnoreCase("-s") == 0) {
                    CreateLogFile(LogType.Security, "securityLog_" + Long.toString(cal.getTimeInMillis()) + ".log");
                    argIdentified = true;
                }

                // User Activity is "-u"
                if (s.compareToIgnoreCase("-u") == 0) {
                    CreateLogFile(LogType.Order, "userActivityLog_" + Long.toString(cal.getTimeInMillis()) + ".log");
                    argIdentified = true;
                }

                // Order Activity is "-o"
                if (s.compareToIgnoreCase("-o") == 0) {
                    CreateLogFile (LogType.Shipping, "orderActivityLog_" + Long.toString(cal.getTimeInMillis()) + ".log");
                    argIdentified = true;
                }

            }
            if (!argIdentified) {
                System.out.println("Please enter one or more of the following commands:\n" +
                        "  '-s' : This extracts the security logs to a local file\n" +
                        "  '-u' : This extracts the user activity to a local file\n" +
                        "  '-o' : This extracts the order activity to a local file\n");
            }

    }

    static void CreateLogFile(LogType logType, String fileName)
    {


        com.eep.logging.LoggingService loggingService;
        FileWriter logFile;
        BufferedWriter logStream;
        List <LogEvent> events = null;
        String parsedOutput = new String();

        try
        {
            loggingService = new com.eep.logging.LoggingServiceImpl();

            logFile = new FileWriter (fileName);
            logStream = new BufferedWriter (logFile);

            // call the logging service and get the log
            events = loggingService.retrieveLog(logType);
            if (logType == LogType.Security)
                parsedOutput = "DATE-TIME,USERNAME,EVENT" + "\n";
            if (logType == logType.Order)
                parsedOutput = "DATE-TIME,USERNAME,ORDERID"+ "\n";
            if (logType == logType.Shipping)
                parsedOutput = "DATE-TIME,USERNAME,ORDERID"+ "\n";
            for (int n = 0; n< events.size(); n++)
            {
                // Date and Time need to be added to the logging events and replaced below
                parsedOutput = parsedOutput + events.get(n).getDateTimeStamp() + "," + events.get(n).getUsername() + ",";

                if (logType == LogType.Security)
                    parsedOutput = parsedOutput + events.get(n).getLogEventType() + "\n";
                else
                    parsedOutput = parsedOutput + Integer.toString(events.get(n).getOrderNumber()) + "\n";
            }
            // Use the logging service to write the data out.
            logStream.write(parsedOutput);
            logStream.close();

        }
        catch (Exception e)
        {
            System.err.println ("Error: " + e.getMessage());
        }


    }

}
